package ${package}.${rootArtifactId}.service.account.impl;

import ${package}.${rootArtifactId}.common.constant.NumberConstant;
import ${package}.${rootArtifactId}.common.exception.BusinessException;
import ${package}.${rootArtifactId}.common.model.PageInfoResult;
import ${package}.${rootArtifactId}.common.model.Result;
import ${package}.${rootArtifactId}.common.util.JsonUtil;
import ${package}.${rootArtifactId}.common.util.PageHelper;
import ${package}.${rootArtifactId}.common.util.ValidatorUtil;
import ${package}.${rootArtifactId}.mapper.account.AccountMapper;
import ${package}.${rootArtifactId}.model.param.account.AccountOpParam;
import ${package}.${rootArtifactId}.model.param.account.AccountQueryParam;
import ${package}.${rootArtifactId}.model.po.account.AccountPO;
import ${package}.${rootArtifactId}.service.account.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author weidong.lin
 * @description 账号信息表接口实现层
 * @date 2021-07-20 18:09:29
 */
@Service
@Slf4j
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public PageInfoResult<AccountPO> getPages(AccountQueryParam param) {
        return PageHelper.warpPage(param, accountMapper);
    }

    @Override
    public AccountPO getById(Long id) {
        if (id == null) {
            return null;
        }
        return accountMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result create(AccountOpParam opParam) {
        ValidatorUtil.validate(opParam);
        AccountPO model = JsonUtil.transform(opParam, AccountPO.class);
        // 创建人信息
        int insertResult = accountMapper.insertSelective(model);
        if (NumberConstant.NUM_0L >= insertResult) {
            throw new BusinessException("保存配置信息异常");
        }
        return Result.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result update(AccountOpParam opParam) {
        ValidatorUtil.validate(opParam);
        if (opParam.getId() == null) {
            return Result.failure("主键ID为空");
        }
        // 修改人信息
        AccountPO model = JsonUtil.transform(opParam, AccountPO.class);
        accountMapper.updateByPrimaryKeySelective(model);
        return Result.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result delete(Long id) {
        if (id == null) {
            return Result.failure("主键ID为空");
        }

        AccountPO updateParam = new AccountPO();
        updateParam.setId(id);
        updateParam.setStatus(0);
        updateParam.setUpdateAccount("admin");
        updateParam.setUpdateTime(new Date());
        // 修改人信息
        accountMapper.updateByPrimaryKeySelective(updateParam);

        return Result.success();
    }
}
